package com.zhang.server.controller;

import com.zhang.server.common.Result;
import com.zhang.server.controller.request.BorrowPageRequest;
import com.zhang.server.entity.Borrow;
import com.zhang.server.entity.Retur;
import com.zhang.server.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    IBorrowService borrowService;

    @GetMapping("/list")
    public Result list(){
        return Result.success(borrowService.list());
    }

    @GetMapping("/page")
    public Result page(BorrowPageRequest borrowPageRequest){
        if(borrowPageRequest.getPageNum() == null){
            borrowPageRequest.setPageNum(1);
        }

        if(borrowPageRequest.getPageSize() == null){
            borrowPageRequest.setPageSize(10);
        }
        return Result.success(borrowService.page(borrowPageRequest));
    }

    @GetMapping("/retur/page")
    public Result returPage(BorrowPageRequest borrowPageRequest){
        if(borrowPageRequest.getPageNum() == null){
            borrowPageRequest.setPageNum(1);
        }

        if(borrowPageRequest.getPageSize() == null){
            borrowPageRequest.setPageSize(10);
        }
        return Result.success(borrowService.returPage(borrowPageRequest));
    }

    @PostMapping("/save")
    public Result save(@RequestBody Borrow borrow){
        borrowService.save(borrow);
        return Result.success();
    }

    @PostMapping("/retur/save")
    public Result returSave(@RequestBody Retur retur){
        borrowService.returSave(retur);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return borrowService.getById(id);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Borrow borrow){
        borrowService.update(borrow);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        borrowService.delete(id);
        return Result.success();
    }


    @DeleteMapping("/retur/delete/{id}")
    public Result returDelete(@PathVariable Integer id){
        borrowService.returDelete(id);
        return Result.success();
    }

}
